<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class WaitingTime
 * Desc  中途等待控制器
 * Create on 2024/12/2 15:12
 * Create by wangyafang
 */

namespace app\api\controller\wddj\driver;


use app\common\controller\Api;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class WaitingTime extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['waiting_midway','continue_journey'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\WaitingTime();
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
    }
    
    /**
     * Desc 中途等待
     * Create on 2024/12/2 15:14
     * Create by wangyafang
     */
    public function waiting_midway()
    {
        $order_id = $this->request->post('order_id');
        if (empty($order_id)) {
            $this->error('请输入订单ID');
        }

        $order_state = (new \app\api\model\wddj\driver\Order())
            ->where('id',$order_id)
            ->value('state');
        if (!$order_state) {
            $this->error('订单不存在');
        }

        if ($order_state != 'in_progress') {
            $this->error('不在行驶中，无法操作');
        }

        $waitingTimeObj = $this->model->where('order_id',$order_id)
            ->where('type','waiting_midway')
            ->order('id desc')
            ->find();
        if (!empty($waitingTimeObj) && ($waitingTimeObj['end_time'] == 0)) {
            $this->error('已点击中途等待');
        }


        $result = false;
        $start_time = time();
        Db::startTrans();
        try {
            $waiting_time_data = array(
                'order_id'=>$order_id,
                'type'=>'waiting_midway',
                'free_waiting_time'=>0,
                'end_time'=>0,
                'start_time'=>$start_time,
                'minute'=>0,
            );
            $this->model->data($waiting_time_data);
            $result = $this->model->allowField(true)->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if (false === $result) {
            $this->error('中途等待操作失败');
        }

        $this->success('中途等待操作成功',array(
            'id'=>$this->model->id,
            'waiting_start_time'=>$start_time,
        ));
    }

    /**
     * Desc 继续行程
     * Create on 2024/12/2 15:24
     * Create by wangyafang
     */
    public function continue_journey()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $order_id = $this->request->post('id');
        $waitingTimeObj = $this->model
            ->where('order_id',$order_id)
            ->where('type','waiting_midway')
            ->where('end_time',0)
            ->order('id desc')
            ->find();

        if (empty($waitingTimeObj)) {
            $this->error('中途等待数据不存在');
        }

        if (!empty($waitingTimeObj['end_time'])) {
            $this->error('已点击继续行程');
        }

        $order_state = (new \app\api\model\wddj\driver\Order())
            ->where('id',$waitingTimeObj['order_id'])
            ->value('state');
        if (!$order_state) {
            $this->error('订单不存在');
        }

        if ($order_state != 'in_progress') {
            $this->error('不在行驶中，无法操作');
        }

        $end_time = time();
        $update_data = array(
            'end_time'=>$end_time,
            'minute'=>wddj_get_waiting_time($end_time,$waitingTimeObj['start_time']),
        );
        $result = false;
        Db::startTrans();
        try {
            $result = $waitingTimeObj->save($update_data);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        if (false === $result) {
            $this->error('继续行程操作失败');
        }

        $this->success('继续行程操作成功');
    }
}



 